<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务" prop="taskId">
        <el-select v-model="queryParams.taskId" placeholder="请选择任务" clearable size="small">
          <el-option
            v-for="task in tasks"
            :key="task.id"
            :label="task.name"
            :value="task.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="ownerId">
        <el-select v-model="queryParams.ownerId" placeholder="请选择负责人" clearable size="small">
          <el-option
            v-for="user in users"
            :key="user.userId"
            :label="user.userName"
            :value="user.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="historyList">
      <el-table-column label="任务" align="center" prop="taskId">
        <template slot-scope="scope">
          <span>{{ getTaskName(scope.row.taskId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="ownerId">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.ownerId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分配人" align="center" prop="assignedBy">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.assignedBy) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分配时间" align="center" prop="assignedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignedAt) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listTask, listHistory} from "@/api/hanyun/task";

export default {
  name: "TaskHistory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史记录表格数据
      historyList: [],
      // 任务列表
      tasks: [],
      // 用户列表
      users: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        ownerId: null
      }
    };
  },
  created() {
    this.getList();
    this.getTasks();
    this.getUsers();
  },
  methods: {
    /** 查询历史记录列表 */
    getList() {
      this.loading = true;
      listHistory(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取任务列表 */
    getTasks() {
      listTask().then(response => {
        this.tasks = response.rows;
      });
    },
    /** 获取用户列表 */
    getUsers() {
      // TODO: 调用获取用户列表接口
      this.users = [];
    },
    /** 获取任务名称 */
    getTaskName(taskId) {
      const task = this.tasks.find(t => t.id === taskId);
      return task ? task.name : taskId;
    },
    /** 获取用户名称 */
    getUserName(userId) {
      const user = this.users.find(u => u.userId === userId);
      return user ? user.userName : userId;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
